package com.example.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class FileUtil {

    public static String saveMovie(MultipartFile file,String uploadPath){
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        try {
            Path path = Paths.get(uploadPath + filename);
            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);

            String imageUrl = "http://localhost/images/movie/" + filename;
            return imageUrl;
        } catch (IOException e) {
            throw new RuntimeException("Failed to store file: " + e.getMessage());
        }
    }

    public static String saveBanner(MultipartFile file,String uploadPath){
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        try {
            Path path = Paths.get(uploadPath + filename);
            Files.copy(file.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING);

            String imageUrl = "http://localhost/images/banner/" + filename;
            return imageUrl;
        } catch (IOException e) {
            throw new RuntimeException("Failed to store file: " + e.getMessage());
        }
    }
}
